package com.gisuni.sddc.platformManager.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.gisuni.sddc.platformManager.model.Organization;
import com.gisuni.sddc.platformManager.model.ProjectType;
import com.gisuni.sddc.platformManager.model.ProjectTypeTimenode;
import com.gisuni.sddc.platformManager.model.Response;
import com.gisuni.sddc.platformManager.model.RoleInfo;
import com.gisuni.sddc.platformManager.model.RoleRModule;
import com.gisuni.sddc.platformManager.model.RoleRResource;
import com.gisuni.sddc.util.StringUtil;

@IocBean
public class ProjectTypeDao {
	
	@Inject
	private Dao gisplatform;
	@Inject
	private BasicDao basicDao;
 	
 
 
    public Boolean executeSQL(String updateSql ){
    	
    	Boolean result = false;
    	Sql sql = Sqls.create(updateSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				return true;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(Boolean.class );
 
    }
 
 
	public String querySql(String  selectSql) {
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				JSONArray result=new JSONArray();
				int numberOfColumns = rs.getMetaData().getColumnCount();
			     while (rs.next()){
			    	 JSONObject ret=new JSONObject();
						// 遍历查询结果字段
						for (int i = 0; i < numberOfColumns; i++) {
							Object obj = rs.getString(i + 1);
							ret.put(rs.getMetaData().getColumnName(i+1).trim(), obj);
						}
			    	 result.put(ret);
			     }
				return result.toString();
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(String.class);
	}
	
	
	public String getControlRegion(String roleName){
		String result = "";
		if(!StringUtil.isNullOrEmpty(roleName)){
			 
			
		}
		return result;
	}


	public List<ProjectType> findEntity(String selectstr) {
		Sql sql = Sqls.create(selectstr);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				List<ProjectType> result = new ArrayList<ProjectType>();
			     while (rs.next()){
			    	 ProjectType entity=new ProjectType();
			    	 if(rs.findColumn("ID")>0) entity.setId(rs.getString("ID"));
			    	 if(rs.findColumn("PROJECT_TYPE_NAME")>0) entity.setProjectTypeName(rs.getString("PROJECT_TYPE_NAME"));
			    	 result.add(entity);
			     }
			     
				return result;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(List.class);
	    
	}


	public String queryBaseEntityAll(int from, int to, String sql) {
		return  querySql(sql);
	}
	
	public int queryBaseEntityCount( String roleName) {
		Cnd cnd = null;
		if(!StringUtil.isNullOrEmpty(roleName)){
			cnd = Cnd.where("ROLE_NAME","LIKE","%"+roleName+"%");
		}
		List<RoleInfo> listSec =gisplatform.query(RoleInfo.class, cnd, null);
		
		if(listSec!=null && listSec.size()>0){
			return listSec.size();
		}else{
			return 0;
		}
	}



	public boolean addEntity(RoleInfo role) {
		RoleInfo t = gisplatform.insert(role);
		if(t!=null){
			return true;
		}
		return false;
	}



	public boolean updateEntity(RoleInfo role) {
		if(gisplatform.updateIgnoreNull(role)>0)
			return true;
		return false;
	}



	public boolean delEntity(RoleInfo role) {
		int res = gisplatform.delete(role);
		if(res>0){
			return true;
		}
		return false;
	}



	public RoleInfo findEntityById(String id) {
		RoleInfo role = new RoleInfo();
		role.setRoleId(id);
//		gisplatform.getEntity(org)
//		List<Record>  re  = gisplatform.query("ORGANIZATION", Cnd.where("ID","=",id));
		RoleInfo res = gisplatform.fetch(RoleInfo.class,id );
 
		return res;
	}


	public Boolean addRoleRModule(RoleRModule entity) {
		RoleRModule t = gisplatform.insert(entity);
		if(t!=null){
			return true;
		}
		return false;
		
	}


	public boolean addRoleRResource(RoleRResource entity) {
		RoleRResource t = gisplatform.insert(entity);
		if(t!=null){
			return true;
		}
		return false;
	}


	public ProjectType addProjectType(ProjectType entity) {
		return gisplatform.insert(entity);
	}
	
	public ProjectTypeTimenode addProjectTypeTimenode(ProjectTypeTimenode entity) {
		return gisplatform.insert(entity);
	}

	public ProjectType getWYTZType(){
		Cnd cnd = Cnd.where("flag","=","WYTZ");
		return basicDao.findByCondition(ProjectType.class, cnd);
	}
	
}
